package com.ruoyi.business.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.business.domain.ShopVerification;
import com.ruoyi.business.mapper.ShopVerificationMapper;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.Coupon;
import com.ruoyi.business.service.ICouponService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 平台优惠券Controller
 * 
 * @author ffc
 * @date 2024-09-12
 */
@RestController
@RequestMapping("/couponReal")
public class CouponController extends BaseController
{
    @Autowired
    private ICouponService couponService;

    @Autowired
    private ShopVerificationMapper shopVerificationMapper;

    /**
     * 查询平台优惠券列表
     */
    @RequiresPermissions("business:couponReal:list")
    @GetMapping("/list")
    public TableDataInfo list(Coupon coupon)
    {
        String username = SecurityUtils.getUsername();
        QueryWrapper<ShopVerification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",username);
        ShopVerification shopVerification = shopVerificationMapper.selectOne(queryWrapper);
        if(shopVerification!=null){
            coupon.setMid(shopVerification.getMid());
        }
        startPage();
        List<Coupon> list = couponService.selectCouponList(coupon);
        return getDataTable(list);
    }

    /**
     * 导出平台优惠券列表
     */
    @RequiresPermissions("business:couponReal:export")
    @Log(title = "平台优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coupon coupon)
    {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.exportExcel(response, list, "平台优惠券数据");
    }

    /**
     * 获取平台优惠券详细信息
     */
    @RequiresPermissions("business:couponReal:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(couponService.selectCouponById(id));
    }

    /**
     * 新增平台优惠券
     */
    @RequiresPermissions("business:couponReal:add")
    @Log(title = "平台优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改平台优惠券
     */
    @RequiresPermissions("business:couponReal:edit")
    @Log(title = "平台优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 领取优惠券
     */
    @RequiresPermissions("business:couponReal:edit")
    @Log(title = "领取优惠券", businessType = BusinessType.UPDATE)
    @PutMapping("/{uid}/{id}")
    public AjaxResult getCoupon(@PathVariable("id") Long id,@PathVariable("uid") Long uid)
    {
        return toAjax(couponService.getCoupon(id,uid));
    }

    /**
     * 删除平台优惠券
     */
    @RequiresPermissions("business:couponReal:remove")
    @Log(title = "平台优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(couponService.deleteCouponByIds(ids));
    }
}
